//
//  WebServicesUtils.m
//  Bus
//
//  Created by wangjiajia on 13-7-10.
//  Copyright (c) 2013年 wangjiajia. All rights reserved.
//

#import "WebServicesUtils.h"
#import "LoginResult.h"
#import "ASIHTTPRequest.h"
#import "ASIHTTPRequestDelegate.h"

#import "MBProgressHUD.h"
@implementation WebServicesUtils
+(id)voxx{return nil;}
# pragma mark -同步方法synchronize -test -deprecated
+(id)checkLogin:(NSString *)userName andPass:(NSString *)password{
    
    //设置请求地址
    NSURL *apiurl=[NSURL URLWithString:api_checkLogin];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:apiurl];
    //请求头文件,设置为Json
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    //json请求的参数必须为json格式，而非get请求格式floginid=%@&fpassword=%@,否则请求错误！
    NSString * postStr=[NSString stringWithFormat:@"{\"floginid\":\"%@\",\"fpassword\":\"%@\"}",userName,password];
    //转为二进制数据
    NSData *postData=[postStr dataUsingEncoding:NSUTF8StringEncoding];
    //设置请求体和请求方式
    [request setHTTPBody:postData];
    [request setHTTPMethod:@"post"];
    
    //发送同步请求，得到响应结果（可能阻塞UI线程）
    NSData *response=[NSURLConnection sendSynchronousRequest:request returningResponse:	nil error:nil];
    //NSLog(@"响应数据response转为字符串-->%@",[[NSMutableString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
    
    //将响应结果解析后放入字典中
    NSDictionary *rootDict=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil];
    //NSLog(@"响应结果转为字典rootDict-->%@",rootDict); 
    
  
    
    //.net3.5 /.net4.0 返回格式:{"d":"{结果}"}
    NSDictionary* resultDict=[rootDict objectForKey:@"d"];//测试了3小时左右，为该句错误，这里取得的是一个字符串(弱类型，没有java明显)，无法转化成有效的NSDictionary。
     //NSLog(@"resultDict无效格式，其实是个字符串-->%@",resultDict);
    if(nil==resultDict){
        //没有取到@“d”的值，说明为低版本的 .net2.0 / .net 3.0  返回格式为:{结果}
        //resultDict=rootDict;
    }
    //正确写法
    NSString * allStr=[rootDict objectForKey:@"d"] ;
    //NSLog(@"(注意格式问题)\n返回的Json字符串为：%@",allStr);
    resultDict=[NSJSONSerialization JSONObjectWithData:[allStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
    
    //NSLog(@"resultDict有效格式-->%@",resultDict);
    
    //以下一条语句报错：'-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x7578f60'
    //未识别的方法发送给实例对象，也即该实例对象没有这个方法。是因为返回的Json格式不规范，从而没有转换成Dict，却调用了Dict方法 造成。
    //NSLog(@"%d",[[resultDict valueForKey:@"Ret"] intValue] );
    //NSLog(@"%@",[resultDict objectForKey:@"Info"] );
    
    LoginResult *result=[[LoginResult alloc] initWithLoginRet:[[resultDict objectForKey:@"Ret"] intValue]  andLoginMsg:[resultDict objectForKey:@"Info"]andUserId:[[resultDict objectForKey:@"UserID"] intValue] andUserName:[resultDict objectForKey:@"UserName"]  ];
    return  result;
}

#pragma mark- 以下为使用ASI实现的异步方法 asynchronize

#pragma mark 验证登陆
+(ASIHTTPRequest*)checkLogin:(NSString *)userName andPass:(NSString *)password withDelegate:(id<ASIHTTPRequestDelegate>) delegate{
    //设置请求地址
    NSURL *apiurl=[NSURL URLWithString:api_checkLogin];
    //构建请求对象
    ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:apiurl];
    //设置请求头参数，如果没有该请求头会出现:请求格式无效。非常重要!
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    //设置指示器
    //request.hud=[[MBProgressHUD alloc] initWithView:self.view];
    //请求自带了指示器hud，可以把hud赋值进去，另外还要把当前view传递过去，比较麻烦
    //当然代理即为控制器对象，可以强制转换

    //请求参数
    NSString * postStr=[NSString stringWithFormat:@"{\"floginid\":\"%@\",\"fpassword\":\"%@\"}",userName,password];
    
    //NSString * postStr=[NSString stringWithFormat:@"floginid=%@&fpassword=%@",userName,password];//会照成处理请求时出错
    
    
    [request appendPostData:[postStr  dataUsingEncoding:NSUTF8StringEncoding]] ;
    
    
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark  支持分页获取新闻数据
+(ASIHTTPRequest*)getNewsInfoPageIndex:(NSInteger)pageIndex andPageSize:(NSInteger)pageSize newsType:(NewsType)type withDelegate:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getNewsInfo];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    
    NSString * postStr=[NSString stringWithFormat:@"{\"newsType\":\"%i\",\"pageFor\":\"%i\",\"pageSize\":\"%i\"}",type,pageIndex,pageSize];
    [request appendPostData:[postStr  dataUsingEncoding:NSUTF8StringEncoding]] ;  
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}

#pragma mark 根据ID获取新闻内容
+(ASIHTTPRequest*)getNewInfoByID:(int)articleID withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getDetailInfo];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    
    NSString * postStr=[NSString stringWithFormat:@"{\"articleID\":\"%i\"}",articleID];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}

#pragma mark- 获取财务信息
+(ASIHTTPRequest*)getProfit:(int)year andMonth:(int)month withDelege:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getProfit];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    
    NSString * postStr=[NSString stringWithFormat:@"{\"fyear\":\"%i\",\"fperiod\":\"%i\"}",year,month];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}

#pragma mark- 支持分页获取汇报时间
+(ASIHTTPRequest*)getRepTimeList:(int)pageFor andPageSize:(int)pageSize withDelegate:(id<ASIHTTPRequestDelegate>)delegate{

        NSURL* apiurl=[NSURL URLWithString:api_getRepTimeList];
        ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
        [request addRequestHeader:@"Content-Type" value:@"application/json"];
        
        NSString * postStr=[NSString stringWithFormat:@"{\"pageFor\":\"%i\",\"pageSize\":\"%i\"}",pageFor,pageSize];
        [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
        [request setDelegate:delegate];
        [request startAsynchronous];
return request;
}

#pragma mark- 获取汇报部门
+(ASIHTTPRequest*)getRepDepList:(NSString *)repTime withDelegate:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getRepDepList];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    
    NSString * postStr=[NSString stringWithFormat:@"{\"repTime\":\"%@\"}",repTime];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}


#pragma mark- 根据ID获取新闻内容
+(ASIHTTPRequest*)getRepDetailByID:(int)repID withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getRepDetail];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    
    NSString * postStr=[NSString stringWithFormat:@"{\"repID\":\"%i\"}",repID];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark - -------工作流核心接口--------
#pragma mark - 获取表单类型

+(ASIHTTPRequest*)getWFFormWithDelegate:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getWFForm];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request setTag:1];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request appendPostData:[@"{\"id\":\"1\"}" dataUsingEncoding:NSUTF8StringEncoding]];//即使无参数，也要随便加一个
    [request setDelegate:delegate];
    [request startAsynchronous];
  
    return request;
}

+(ASIHTTPRequest *)getUserFormByUserID:(int)userId andFormName:(NSString *)formName andState:(NSString *)state andPageFor:(int)pageFor andPageSize:(int)pageSize withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_getUserForm];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request setTag:2];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString* postStr=[NSString stringWithFormat:@"{userid:%i,formname:\"%@\",state:\"%@\",pageFor:%i,pageSize:%i}",userId,formName,state,pageFor,pageSize];//参数名可以不用\",参数名大小写铭感 ; 参数值为数字 可以不用\"; 参数值为字符串必须\"
    //NSLog(@"查询参数%@",postStr);
    
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    
    return request;
}
#pragma mark 根据ID获取表单内容 私用方法,tag可以写死
+(ASIHTTPRequest*)getUserFormByID:(int)Id andState:(NSString*) state withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    //NSLog(@"查询参数{\"state\":\"%@\",\"id\":\"%i\"}",state,Id);
    NSURL* apiurl=[NSURL URLWithString:api_getSingleUserForm];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    request.tag=0;
    NSString * postStr=[NSString stringWithFormat:@"{\"state\":\"%@\",\"id\":\"%i\"}",state,Id];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- 获取只读表单名列表
+(ASIHTTPRequest*) getReadOnlyFormName:(NSString *)execSql withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    //NSLog(@"执行语句%@",execSql);
    NSURL* apiurl=[NSURL URLWithString:api_execSql];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=1;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{\"sql\":\"%@\"}",execSql];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}



#pragma mark- 保存
+(ASIHTTPRequest *)processSave:(NSString *)formId andUserId:(int) userId andUserName:(NSString *)userName andFileContent:(NSString *)fileContent andSpContent:(NSString *)spContent withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_processSave];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=2;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%d,userName:\"%@\",fileContent:\"%@\",spContent:\"%@\"}",formId,userId,userName,fileContent,spContent];
    ////NSLog(@"postStr=%@",postStr);
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- 保存并通过
+(ASIHTTPRequest *)processSaveAndPass:(NSString *)formId andUserId:(int)userId andUserName:(NSString *)userName andFileContent:(NSString *)fileContent andSpContent:(NSString *)spContent withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_processSaveAndPass];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=3;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%d,userName:\"%@\",fileContent:\"%@\",spContent:\"%@\"}",formId,userId,userName,fileContent,spContent];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- 保存并驳回
+(ASIHTTPRequest *)processSaveAndReject:(NSString *)formId andUserId:(int)userId andUserName:(NSString *)userName andFileContent:(NSString *)fileContent andSpContent:(NSString *)spContent withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_processSaveAndReject];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=4;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%d,userName:\"%@\",fileContent:\"%@\",spContent:\"%@\"}",formId,userId,userName,fileContent,spContent];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;

}



#pragma mark- exec 公用方法,tag不能写死了
+(ASIHTTPRequest*)getExec:(NSString*) execSql andTag:(int)tag withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    //NSLog(@"执行语句%@",execSql);
    NSURL* apiurl=[NSURL URLWithString:api_execSql];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=tag;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{\"sql\":\"%@\"}",execSql];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- execsingle 公用方法,tag不能写死了
+(ASIHTTPRequest*)getExecSingle:(NSString*) execSql andTag:(int)tag withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    //NSLog(@"执行语句%@",execSql);
    NSURL* apiurl=[NSURL URLWithString:api_execSqlSingle];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=tag;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{\"sql\":\"%@\"}",execSql];
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}

#pragma mark- processSubmit
+(ASIHTTPRequest *)processSubmit:(NSString *)formId andUserId:(NSString *)userId andUserName:(NSString *)userName andJbrUserId:(NSString *)jbrUserId andJbrUserName:(NSString *)jbrUserName andSelectNodeValue:(NSString *)selectNodeValue withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_processSubmit];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=1;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%@,userName:\"%@\",jbrUserId:\"%@\",jbrUserName:\"%@\",selectedValue:\"%@\",innerSms:\"%@\",phoneSms:\"%@\"}",formId,userId,userName,jbrUserId,jbrUserName,selectNodeValue,@"1",@"1"];
     ////NSLog(@"postStr=%@",postStr);
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- processReject
+(ASIHTTPRequest *)processReject:(NSString *)formId andUserId:(NSString *)userId andUserName:(NSString *)userName andJbrUserId:(NSString *)jbrUserId andJbrUserName:(NSString *)jbrUserName andSelectNodeValue:(NSString *)selectNodeValue withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    NSURL* apiurl=[NSURL URLWithString:api_processReject];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=1;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%@,userName:\"%@\",jbrUserId:\"%@\",jbrUserName:\"%@\",selectedValue:\"%@\"}",formId,userId,userName,jbrUserId,jbrUserName,selectNodeValue];
    ////NSLog(@"postStr=%@",postStr);
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;
}
#pragma mark- processWt
+(ASIHTTPRequest *)processWt:(NSString *)formId andUserId:(NSString *)userId andUserName:(NSString *)userName andWtUserId:(NSString *)wtUserId andWtUserName:(NSString *)wtUserName withDelegete:(id<ASIHTTPRequestDelegate>)delegate{
    
    NSURL* apiurl=[NSURL URLWithString:api_processWt];
    ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:apiurl];
    request.tag=1;
    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    NSString * postStr=[NSString stringWithFormat:@"{id:%@,userId:%@,userName:\"%@\",wtUserId:\"%@\",wtUserName:\"%@\"}",formId,userId,userName,wtUserId,wtUserName];//服务器上是userID,wtUserID ,要同名,不可写成userId,wtUserId. 否则{"Message":"处理请求时出错。","StackTrace":"","ExceptionType":""}。后来修改服务器上的了。

    ////NSLog(@"postStr=%@",postStr);
    [request appendPostData:[postStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setDelegate:delegate];
    [request startAsynchronous];
    return request;

}

@end
